# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=glfw
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.3.2
pkgrel=1
pkgdesc="A free, open source, portable framework for OpenGL application development (mingw-w64)"
arch=('any')
url="https://www.glfw.org/"
license=('custom:ZLIB')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers")
options=('staticlibs' 'strip')
source=(https://downloads.sourceforge.net/project/glfw/glfw/${pkgver}/${_realname}-${pkgver}.tar.gz
        001-3.2-cmake-suffix.patch
        003-3.2-cmake-install.patch)
sha256sums=('aa9a2325c3d2cb3a18b236326b3f866545a46cd48514b00c999fdd9374a7325b'
            '9eb7f6d494ff810fc5d4c0939545ce98fa2a4541e538146a1f85227328b430b0'
            '706d7b81d0e37e881bef33bcd330a89f7fa63031fbeb8b4f1790f900fa803739')

prepare() {
  cd "${srcdir}/glfw-${pkgver}"
  patch -Np1 -i ${srcdir}/001-3.2-cmake-suffix.patch
  patch -Np1 -i ${srcdir}/003-3.2-cmake-install.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # Build shared libs
  [[ -d ${srcdir}/shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/shared-${MINGW_CHOST}
  mkdir -p ${srcdir}/shared-${MINGW_CHOST}
  cd ${srcdir}/shared-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DCMAKE_INSTALL_LIBDIR=lib \
    -DCMAKE_C_COMPILER=${MINGW_CHOST}-gcc \
    -DBUILD_SHARED_LIBS=ON \
    -DGLFW_BUILD_DOCS=OFF \
    ../${_realname}-${pkgver}
  make

  # Build static libs
  [[ -d ${srcdir}/static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/static-${MINGW_CHOST}
  mkdir -p ${srcdir}/static-${MINGW_CHOST}
  cd ${srcdir}/static-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DCMAKE_INSTALL_LIBDIR=lib \
    -DCMAKE_C_COMPILER=${MINGW_CHOST}-gcc \
    -DBUILD_SHARED_LIBS=OFF \
    -DGLFW_BUILD_DOCS=OFF \
    ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}/static-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  cd "${srcdir}/shared-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  # License
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
